package part_1;

import java.text.DecimalFormat;

/*
   Purpose:
     This class stores and manipulates dates on the
     Gregorian calendar.  It implements constructors,
     set methods, get methods, and predicate methods,
     and overrides the toString method.

     Method list:
        Date()                   Date constructor
        Date(day,month,year)     Date constructor
        setDate(day,month,year)  Set Date
        getDay()                 Get day
        getMonth()               Get month
        getYear()                Get year
        isLeapYear()             Test for leap year
        isEqual()                Test for equality
        isEarlier()              Is chronologically earler
        isLater()                Is chronologically later
        toString()               Convert to string for display

   Record of revisions:
       Date       Programmer          Description of change
       ====       ==========          =====================
       05/01/2002 S. J. Chapman       Original code
       16/05/2008 S Billemont		  Added the priorDay() meth
    								  changed the tostring to give the month in a word
*/
public class Date {

   // Define instance variables
   private int year;       // Year (0 - xxxx)
   private int month;      // Month (1 - 12)
   private int day;        // Day (1 - 31)

   // Default constructor produces January 1, 1900
   public Date() {
      year = 1900;
      month = 1;
      day = 1;
   }

   // Constructor for specified date
   public Date(int day, int month, int year) {
      setDate( day, month, year );
   }

   // Method to set a date
   public void setDate(int day, int month, int year) {
      this.year  = year;
      this.month = month;
      this.day   = day;
   }

   // Method to get day
   public int getDay() {
      return day;
   }

   // Method to get month
   public int getMonth() {
      return month;
   }

   // Method to get year
   public int getYear() {
      return year;
   }

   // Method to check for leap year
   public boolean isLeapYear() {
      boolean leap_year;
      if ( year % 400 == 0 )
         leap_year = true;
      else if ( year % 100 == 0 )
         leap_year = false;
      else if ( year % 4 == 0 )
         leap_year = true;
      else
         leap_year = false;
      return leap_year;
   }

   // Method to check for equality
   public boolean isEqual( Date d ) {
      boolean equal;
      if ( year == d.year && month == d.month &&
           day == d.day )
         equal = true;
      else
         equal = false;
      return equal;
   }

   // Method to check if the date stored in this
   // object is earlier than the Date d.
   public boolean isEarlier( Date d ) {
      boolean earlier;

      // Compare years
      if ( year > d.year )
         earlier = false;
      else if ( year < d.year )
         earlier = true;
      else {

         // Years are equal.  Compare months
         if ( month > d.month )
            earlier = false;
         else if ( month < d.month )
            earlier = true;
         else {

            // Months are equal.  Compare days.
            if ( day >= d.day )
               earlier = false;
            else
               earlier = true;
         }
      }
      return earlier;
   }

   // Method to check if the date stored in this
   // object is later than the Date d.
   public boolean isLater( Date d ) {
      boolean later;

      // Compare years
      if ( year > d.year )
         later = true;
      else if ( year < d.year )
         later = false;
      else {

         // Years are equal.  Compare months
         if ( month > d.month )
            later = true;
         else if ( month < d.month )
            later = false;
         else {

            // Months are equal.  Compare days.
            if ( day > d.day )
               later = true;
            else
               later = false;
         }
      }
      return later;
   }
   
   // Reduce the date by one day
	public void priorDay() {
		int[] monthLength = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		
		//is it the first day?
		if (this.day != 1){
			this.day --;
		} else {
			//is it the first month?
			if (this.month != 1){
				this.month --;
			} else {
				this.month = 12;
				this.year--;
			}
			//check for leapyear && feb
			if(isLeapYear()&& this.month == 2){
				this.day = 29;
			} else {
				this.day = monthLength[month-1];
			}
		}		
	}
   
   // Method to convert a date to a string.
   public String toString() {
	   DecimalFormat df = new DecimalFormat("00");
	   String[] monthNames = {"January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
       return (df.format(day) + "-" + monthNames[month-1] + "-" + year);
   }
}
